package com.example.mywmb

import android.util.Log

import android.content.Intent
import android.content.pm.PackageManager
import android.os.Bundle
import android.widget.Button
import android.widget.TextView
import android.widget.Toast
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat

class MainActivity : AppCompatActivity() {
    
    private lateinit var btnStartDevice: Button
    private lateinit var tvVersionNumber: TextView
    private lateinit var relayController: RelayController
    
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        setContentView(R.layout.activity_main)
        
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main)) { v, insets ->
            val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)
            insets
        }
        
        initViews()
        setupListeners()
        
        // 初始化继电器控制器
        relayController = RelayController()
        
        // 打开app时，默认打开继电器1，其他关闭
        try {
            // 先关闭所有继电器
            relayController.closeAllRelays()
            // 然后只打开继电器1
            relayController.controlRelay(1, true)
            Log.d("MainActivity", "默认状态设置完成：继电器1开启，其他关闭")
        } catch (e: Exception) {
            Log.e("MainActivity", "设置默认继电器状态失败: ${e.message}")
        }
        
        // 显示版本号
        val version = getAppVersion()
        tvVersionNumber.text = "版本号:$version"

    }
    
    private fun initViews() {
        btnStartDevice = findViewById(R.id.btnStartDevice)
        tvVersionNumber = findViewById(R.id.tvVersionNumber)
    }
    
    private fun getAppVersion(): String {
        return try {
            val packageInfo = packageManager.getPackageInfo(packageName, 0)
            packageInfo.versionName ?: "未知版本"
        } catch (e: PackageManager.NameNotFoundException) {
            "未知版本"
        }
    }
    
    private fun setupListeners() {
        btnStartDevice.setOnClickListener {
            startDevice()
        }

    }
    
    private fun startDevice() {
        try {
//            Toast.makeText(this, "正在启动设备...", Toast.LENGTH_SHORT).show()
            
            // 关闭继电器1，打开继电器2、3、4
            relayController.controlRelay(1, false)
            relayController.controlMultipleRelays(listOf(2, 3, 4), true)
            Log.d("MainActivity", "启动设备：继电器1关闭，继电器2、3、4开启")
            
//            Toast.makeText(this, "设备启动成功！", Toast.LENGTH_SHORT).show()
            
            // 跳转到监控页面
            val intent = Intent(this, MonitoringActivity::class.java)
            startActivity(intent)
            
        } catch (e: Exception) {
            Toast.makeText(this, "设备启动失败: ${e.message}", Toast.LENGTH_LONG).show()
        }
    }
}